package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.RecommendMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.Recommend;
import com.example.demo.model.User;
import com.example.demo.service.RecommendService;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


@Service
public class RecommnendImpl extends ServiceImpl<RecommendMapper, Recommend> implements RecommendService {
    public long add(Recommend recommend) {
        this.save(recommend);
        return recommend.getId();
    }

    public boolean isExists(String url) {
        QueryWrapper<Recommend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url", url);
        Recommend recommend = this.baseMapper.selectOne(queryWrapper);
        if (recommend == null)
            return false;
        return true;
    }
    //随机推荐
    public List<Recommend> randomRecommend() {
        QueryWrapper<Recommend> queryWrapper = new QueryWrapper<>();
        // 添加条件，只选择 keywords 不为 "[]" 的记录
        queryWrapper.ne("keywords", "[]");
        queryWrapper.last("ORDER BY RAND() LIMIT 6");
        return this.baseMapper.selectList(queryWrapper);
    }
    //根据关键词推荐
    public List<Recommend> RecommendByKeywords(String keywords) {
        QueryWrapper<Recommend> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("keywords", "%" + keywords + "%");
        // 随机返回9条
        queryWrapper.last("ORDER BY RAND() LIMIT 9");
        return this.baseMapper.selectList(queryWrapper);
    }

    //根据URL查找Recommend
    public Recommend queryRecommendByUrl(String url) {
        QueryWrapper<Recommend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url", url);
        return this.baseMapper.selectOne(queryWrapper);
    }
}
